package com.yxhl.zoume.core.tripsmgmt.presenter.detail;

import android.content.Context;

import com.yxhl.protobuf.BizOrderRequest;
import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.ClientType;
import com.yxhl.protobuf.CommonReqParams;
import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.tripsmgmt.view.detail.DetailOrderView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.tripsmgmt.DetailTripOrderUseCase;

import javax.inject.Inject;


/**
 * Created by sugarya on 16/5/4.
 * 获取行程详情
 */
public class DetailTripContainerPresenter extends BasePresenter implements IPresenter {

    private static final String TAG = "DetailTripContainerPresenter";

    @Inject
    DetailTripOrderUseCase mDetailTripOrderUseCase;

    private DetailOrderView mDetailOrderView;


    @Inject
    public DetailTripContainerPresenter(Context context) {
        super(context);
    }

    /**
     * 获取行程订单详情
     *
     * @param orderId
     */
    public void getDetailTripOrder(String orderId) {

        CommonReqParams.Builder commonReqParamsBuilder = CommonReqParams.newBuilder();
        commonReqParamsBuilder
                .setClientSystem(CLIENT_SYSTEM)
                .setClientType(ClientType.USER)
                .setClientVersion(getClientVersionName())
                .setToken(getToken());
        final BizOrderRequest.Builder bizOrderRequestBuilder = BizOrderRequest.newBuilder();
        bizOrderRequestBuilder.setCommonReqParams(commonReqParamsBuilder)
                .setOrderSerial(orderId)
                .setMobile(getMobile());

        BizOrderRequest bizOrderRequest = bizOrderRequestBuilder.build();

        mDetailTripOrderUseCase.execute(bizOrderRequest,new ZMSubscriber<BizOrderResponse>() {
                               @Override
                               public void onCompleted() {
                                   super.onCompleted();
                               }

                               @Override
                               public void onError(Throwable e) {
                                   super.onError(e);
                               }

                               @Override
                               public void onNext(BizOrderResponse bizOrderResponse) {
                                   super.onNext(bizOrderResponse);
                                   if (bizOrderResponse != null) {
                                       CommonRespParams commonRespParams = bizOrderResponse.getCommonRespParams();
                                       if (commonRespParams != null) {
                                           String isSucc = commonRespParams.getIsSucc();
                                           if ("true".equalsIgnoreCase(isSucc)) {
                                               mDetailOrderView.renderDetailOrderOnSuccess(bizOrderResponse);
                                           } else {
                                               mDetailOrderView.renderDetailOrderOnFailure();
                                           }
                                       }
                                   }
                               }
                           }
                );
    }


    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onDestroy() {
    }

    @Override
    public void attachView(BaseView view) {
        if (view instanceof DetailOrderView) {
            mDetailOrderView = (DetailOrderView) view;
        }
    }
}
